package com.example.easybuy.controller;

import com.example.easybuy.pojo.Phone;
import com.example.easybuy.pojo.ResponseMessage;
import com.example.easybuy.service.AlipayPaymentService;
import com.example.easybuy.util.AlipayUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Controller
public class AlipayController {

    private Logger logger=Logger.getLogger(getClass());

    @Resource
    private AlipayPaymentService alipayPaymentService;

    @RequestMapping("creatAlipayPhone")
    @CrossOrigin
    public void creatAlipayPhone(HttpServletResponse response, String subject, String phoneNum,String surplus){
        logger.debug("AlipayController-creatAlipayPhone-subject:"+subject+",phoneNum:"+phoneNum+",surplus:"+surplus);
        String titleSub=subject+"-"+phoneNum;
        logger.debug("AlipayController-creatAlipayPhone()-titleSub:"+titleSub);
        ResponseMessage msg = alipayPaymentService.alipayPayment(titleSub,surplus);
        String data = (String)msg.getData();
        logger.debug("AlipayController-creatAlipayPhone-data："+data);
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        try {
            response.getWriter().println(data);
            response.getWriter().close();
        } catch (IOException e) {
            e.printStackTrace();
            logger.debug("AlipayController-creatAlipayPhone-e   error："+e);
        }
    }

    @RequestMapping("AlipaySuccess")
    @CrossOrigin
    @ResponseBody
    public void AlipaySuccess(HttpServletRequest request,HttpServletResponse response) throws IOException {
        logger.debug("AlipayController-AlipaySuccess()-request:"+request);
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");

        ResponseMessage msg=new ResponseMessage();
        PrintWriter writer=response.getWriter();

        try{
            Map<String,String> params=new HashMap<String,String>();
            Map<String,String[]> requestParams=request.getParameterMap();
            if (requestParams==null || requestParams.isEmpty()){
                logger.error("AlipayController-AlipaySuccess()-param is null,param:"+requestParams);
                writer.write("failure");
            }

            //解析支付宝传过来的参数
            for (Iterator<String> iter=requestParams.keySet().iterator();iter.hasNext();){
                String name=(String)iter.next();
                logger.debug("AlipayController-AlipaySuccess()-name："+name);
                String[] values=(String[])requestParams.get(name);
                String valueStr="";
                for (int i=0;i<values.length;i++){
                    valueStr=(i==values.length-1)?valueStr+values[i]:valueStr+values[i]+",";
                }
                logger.debug("AlipayController-AlipaySuccess()-valueStr："+valueStr);
                //乱码解决问题
//                valueStr=new String(valueStr.getBytes("ISO-8859-1"),"utf-8");
                params.put(name,valueStr);
            }
                msg=alipayPaymentService.alipayNotiry(params);
                writer.write(msg.getMsg());
        }catch (Exception e){
                logger.error("AlipayController-AlipaySuccess()-is Error:"+e);
                writer.write("failure");
        }finally {
            writer.close();
        }

    }

    /*
    生成随机数
     */
    @RequestMapping("alipayRandom")
    @ResponseBody
    @CrossOrigin
    public String random(){
        logger.debug("AlipayController-random...start...");
        String alipayRandom=alipayPaymentService.random();
        logger.debug("AlipayController-random-alipayRandom："+alipayRandom);
        return alipayRandom;
    }




}
